﻿using System;
using UnityEngine;

namespace Core
{
    /// <summary>
    /// 前缀 cnt_XXX Button
    /// </summary>
    public class UICounter : UIWidget
    {
#pragma warning disable 0649
        [UIField("txt_value")]
        private readonly UILabel txtValue;
        [UIField("btn_minus")]
        private readonly UIButton btnMinus;
        [UIField("btn_plus")]
        private readonly UIButton btnPlus;
#pragma warning restore 0649

        public Action<int, UILabel> OnChanged { get; set; }
        public Action OnChangeCompleted { get; set; }


        public override void Awake(GameObject go)
        {
            base.Awake(go);

            txtValue.text = "1";
            mValue = 1;

            LongPressListener.Get(btnPlus).onEvent += OnBtnAddLongPressHandler;
            LongPressListener.Get(btnMinus).onEvent += OnBtnReduceLongPressHandler;
        }

        public override void Dispose()
        {
            if (IsDisposed) return;

            LongPressListener.Get(btnPlus).onEvent -= OnBtnAddLongPressHandler;
            LongPressListener.Get(btnMinus).onEvent -= OnBtnReduceLongPressHandler;

            base.Dispose();
        }


        private int mValue;
        public int Value
        {
            get => mValue;
            set
            {
                var v = Mathf.Clamp(value, MinValue, MaxValue);
                if (mValue == v) return;
                SetValue(v);
            }
        }

        public void SetValue(int value)
        {
            var v = Mathf.Clamp(value, MinValue, MaxValue);
            mValue = v;
            txtValue.text = mValue.ToString();
            OnChanged?.Invoke(mValue, txtValue);
            btnPlus.Interactable = mValue != MaxValue;
            btnMinus.Interactable = mValue != MinValue;
        }

        public int MinValue { get; private set; }
        public int MaxValue { get; private set; } = int.MaxValue;

        public void SetRange(int min, int max)
        {
            MinValue = min;
            MaxValue = max;
            if (MinValue > MaxValue)
            {
                MinValue = MaxValue;
            }
            Value = Value;
        }


        private void OnBtnReduceLongPressHandler(LongPressListener.EEvtKind kind, LongPressListener listener)
        {
            switch (kind)
            {
                case LongPressListener.EEvtKind.Down:
                    {
                        Value--;
                    }
                    break;
                case LongPressListener.EEvtKind.Press:
                    {
                        Value -= listener.TickCnt >> 1;
                    }
                    break;
                case LongPressListener.EEvtKind.Up:
                    {
                        OnChangeCompleted?.Invoke();
                    }
                    break;
                default: break;
            }
        }

        private void OnBtnAddLongPressHandler(LongPressListener.EEvtKind kind, LongPressListener listener)
        {
            switch (kind)
            {
                case LongPressListener.EEvtKind.Down:
                    {
                        Value++;
                    }
                    break;
                case LongPressListener.EEvtKind.Press:
                    {
                        Value += listener.TickCnt >> 1;
                    }
                    break;
                case LongPressListener.EEvtKind.Up:
                    {
                        OnChangeCompleted?.Invoke();
                    }
                    break;
                default: break;
            }
        }


    }
}
